package servlet;

import dao.UserDao;
import model.User;
import util.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

//类型
@WebServlet(urlPatterns = "/login.do")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doPost(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String userType = request.getParameter("userType");

        int role=Integer.parseInt(userType);
        UserDao dao=new UserDao();
        User user=new User();
        if(role==1) {
            user = dao.finduser(username, password);

            if (user != null) {
                HttpSession session = request.getSession();
                session.setAttribute("user_id", Integer.toString(user.getId()));
                session.setAttribute("state", Integer.toString(user.getState()));
                session.setAttribute("role", Integer.toString(user.getRole()));

                session.setAttribute("username", username);

                session.setAttribute("description", user.getDescription());
                session.setAttribute("favorsting", user.getFavorString());
                session.setAttribute("gender", user.getGender());

                session.setAttribute("profession", user.getProfession());

                response.sendRedirect("user/home.jsp");
            } else {
                // 密码错误，执行弹窗操作
                response.sendRedirect("login/login_errorpage.jsp");
            }

        }else  if(role==2){
            user = dao.manager(username, password);

            if (user != null) {

                HttpSession session = request.getSession();
                session.setAttribute("user_id", Integer.toString(user.getId()));
                session.setAttribute("state", Integer.toString(user.getState()));
                session.setAttribute("role", Integer.toString(user.getRole()));
                session.setAttribute("username", username);

                session.setAttribute("description", user.getDescription());
                session.setAttribute("favorsting", user.getFavorString());
                session.setAttribute("gender", user.getGender());

                session.setAttribute("profession", user.getProfession());



                response.sendRedirect("admin/home.jsp");
            } else {
                response.sendRedirect("login/login_errorpage.jsp");
            }
        }

    }

    private boolean login(String username, String password) {
        JDBCUtil jdbcUtil = new JDBCUtil();
        String sql="select * from user where username=? and password=? ";
        ResultSet resultSet = jdbcUtil.prepareQuery(sql,username, password);

        try {

            if (resultSet.next()) {
                // 登录成功
                return true;
            } else {
                // 登录失败，账号不存在

                return false;
            }
        } catch (SQLException e) {
            // 处理异常
            e.printStackTrace();
            return false; // 或者抛出自定义异常
        } finally {
            // 关闭数据库资源
            jdbcUtil.closeConnection();
        }
    }
}

